angular.module('inspinia',['uiSwitch','infinity.angular-chosen']).controller("routeMonitorCtrl", function($scope, $http, $state, $stateParams, i18nService,SweetAlert,$document) {
	$scope.routeGridOptions=angular.copy($scope.paginationOptions);
	$scope.addressGridOptions=angular.copy($scope.paginationOptions);
	$scope.accountAddressGridOptions=angular.copy($scope.paginationOptions);
	$scope.accountAddressQuotaGridOptions=angular.copy($scope.paginationOptions);

	i18nService.setCurrentLang('zh-cn');


	$scope.toDay = new Date();

	$scope.allProvinceSelect = [{text:"全部",value:""}];

	$scope.addressType=[{text:"全部",value:-1},{text:"是",value:1},{text:"否",value:0}];

	$scope.getAllProvince = function(){
		//获取所有省份
		$http.get('areaInfo/getAllProvince').success(function(result){
			if(result.status) {
				if(result.data==null || result.data.length==0){
					$scope.allProvinceSelect.push({text:"全部",value:""});
				}else{
					for(var i in result.data){
						$scope.allProvinceSelect.push({text:result.data[i].name,value:result.data[i].id});
					}
				}
			}
		});
	}
	$scope.getAllProvince();

	$scope.routeGrid = {
		paginationPageSize:10,                  //分页数量
		paginationPageSizes: [10,20,50,100],	//切换每页记录数
		useExternalPagination: true,		    //开启拓展名
		enableHorizontalScrollbar: true,        //横向滚动条
		enableVerticalScrollbar : true,  		//纵向滚动条
		columnDefs: [
			{field: 'jumpId',displayName: '路由id',width: 150},
			{field: 'remark',displayName: '备注',width: 200},
			{field: 'targetQuota',displayName: '目标金额',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.targetQuota/10000).toFixed(2)}}</span>'},
			{field: 'transAmount',displayName: '已跑量金额',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.transAmount/10000).toFixed(2)}}</span>'},
			{field: 'accountDate',displayName: '日期',width: 150, cellFilter:'date:"yyyy-MM-dd"'}
		],
		onRegisterApi : function(gridApi) {
			gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
				$scope.routeGridOptions.pageNo = newPage;
				$scope.routeGridOptions.pageSize = pageSize;
				$scope.queryRoute();
			});
		}
	};



	// $scope.routeQueryInfo = {dateStart: "",dateEnd: ""};
	$scope.resetRoute = function() {
		$scope.routeQueryInfo = {dateStart: moment(new Date()).subtract(15,'days').format("YYYY-MM-DD"),dateEnd: moment(new Date()).subtract(1,'days').format("YYYY-MM-DD")};
	};
	$scope.resetRoute();

	$scope.queryRoute = function() {
		var url = "jumpRoute/routeMonitor.do";
		var jumpId = $scope.routeQueryInfo.jumpId ? $scope.routeQueryInfo.jumpId : 0;
		$http.post(url,
			"jumpId="+jumpId+"&dateStart="+$scope.routeQueryInfo.dateStart+"&dateEnd="+$scope.routeQueryInfo.dateEnd+"&pageNo="+$scope.routeGridOptions.pageNo+"&pageSize="+$scope.routeGridOptions.pageSize,
			{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(data){
			$scope.routeGrid.data = data.result;
			$scope.routeGrid.totalItems = data.totalCount;//总记录数
		}).error(function(){
		});
	};

	$scope.queryRoute();

	//注册地监控
	$scope.initAddressGridColumn = [
		{field: 'merProvince',displayName: '注册地',width: 120,pinnable: false,sortable: false},
		{field: 'transAmount',displayName: '已跑量金额(万)',width: 250,pinnable: false,sortable: false}
	]

	// 		{field: 'accountDate',displayName: '日期',width: 200,pinnable: false,sortable: false,cellFilter:'date:"yyyy-MM-dd"'}

	$scope.addressGrid = {
		paginationPageSize:10,                  //分页数量
		paginationPageSizes: [10,20,50,100],	//切换每页记录数
		useExternalPagination: true,		    //开启拓展名
		enableHorizontalScrollbar: true,        //横向滚动条
		enableVerticalScrollbar : true,  		//纵向滚动条
		columnDefs: [
		],
		onRegisterApi: function(gridApi) {
			gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
				$scope.addressGridOptions.pageNo = newPage;
				$scope.addressGridOptions.pageSize = pageSize;
				$scope.queryAddress();
			});
		}
	};

	$scope.resetAddress = function() {
		$scope.addressQueryInfo = {address: "",dateStart: moment(new Date()).subtract(15,'days').format("YYYY-MM-DD"),dateEnd: moment(new Date()).subtract(1,'days').format("YYYY-MM-DD")};
	};
	$scope.resetAddress();
	$scope.queryAddress = function() {
		var url = "jumpRoute/addressMonitor.do";
		$http.post(url,
			"address="+$scope.addressQueryInfo.address+"&dateStart="+$scope.addressQueryInfo.dateStart+"&dateEnd="+$scope.addressQueryInfo.dateEnd+"&pageNo="+$scope.addressGridOptions.pageNo+"&pageSize="+$scope.addressGridOptions.pageSize,
			{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(data){
			var copyColumn = angular.copy($scope.initAddressGridColumn);
			$scope.addressGrid.columnDefs = copyColumn;
			for(var key in data.data.tableHeader){
				var value = data.data.tableHeader[key];
				$scope.addressGrid.columnDefs.push({"field":key,"displayName":value,"width":300,"sortable":false})
			}
			$scope.addressGrid.columnDefs.push({"name":"action","displayName":"账单地","width":200,"sortable":false,cellTemplate:"<a class='lh30' ng-click='grid.appScope.showAddressModal(row)' >账单地交易分布</a>"})
			$scope.addressGrid.columnDefs.push({"field":"accountDate","displayName":"日期","width":200,"sortable":false,"cellFilter":'date:"yyyy-MM-dd"'})

			$scope.addressGrid.data = data.data.gridData;
			$scope.addressGrid.totalItems = data.totalCount;//总记录数
		}).error(function(){
		});
	};
	$scope.queryAddress();


	$scope.addressDetailGrid = {
		data: 'addressDetailMappings',
		columnDefs: [
			{field: 'accountAddress',displayName: '账单地',width: 150},
			{field: 'transAmount',displayName: '已跑量金额(万)',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.transAmount/10000).toFixed(2)}}</span>'},
			{field: 'accountDate',displayName: '日期',width: 150,cellFilter:'date:"yyyy-MM-dd"'}
		],
		onRegisterApi : function(gridApi) {
			$scope.addressDetailGrid = gridApi;
		}
	};

	$scope.addressDetailDate = {};
	$scope.showAddressModal = function(row) {
		if(!row.entity.merProvince){
			$scope.notice("注册地为空，无法查看详情");
			return ;
		}
		$scope.addressDetailDate = {"address": row.entity.merProvince,"date": row.entity.accountDate};
		$http.post("jumpRoute/addressMonitorDetail.do",
			"address="+row.entity.merProvince+"&date="+row.entity.accountDate,
			{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(data){
			if(data.status){
				$scope.addressDetailMappings = data.data;
			}
		}).error(function(){
		});
		$('#addressModal').modal('show');
	}



	$scope.initAccountAddressGridColumn = [
		{field: 'accountAddress',displayName: '账单地',width: 120,pinnable: false,sortable: false},
		{field: 'countQuota',displayName: '总额度(万)',width: 250,pinnable: false,sortable: false},
		{field: 'transAmount',displayName: '已使用额度(万)',width: 250,pinnable: false,sortable: false},
		{field: 'surplusQuota',displayName: '剩余额度(万)',width: 250,pinnable: false,sortable: false},
		{field: 'usePercent',displayName: '使用占比(%)',width: 250,pinnable: false,sortable: false}
	]

	$scope.accountAddressGrid = {
		paginationPageSize:10,                  //分页数量
		paginationPageSizes: [10,20,50,100],	//切换每页记录数
		useExternalPagination: true,		    //开启拓展名
		enableHorizontalScrollbar: true,        //横向滚动条
		enableVerticalScrollbar : true,  		//纵向滚动条
		columnDefs: [
		],
		onRegisterApi: function(gridApi) {
			gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
				$scope.accountAddressGridOptions.pageNo = newPage;
				$scope.accountAddressGridOptions.pageSize = pageSize;
				$scope.queryAccountAddress();
			});
		}
	};

	$scope.resetAccountAddress = function() {
		$scope.accountAddressQueryInfo = {address: "",dateStart: moment(new Date()).subtract(15,'days').format("YYYY-MM-DD"),dateEnd: moment(new Date()).subtract(1,'days').format("YYYY-MM-DD")};
	};
	$scope.resetAccountAddress();
	$scope.queryAccountAddress = function() {
		var url = "jumpRoute/accountAddressMonitor.do";
		$http.post(url,
			"address="+$scope.accountAddressQueryInfo.address+"&dateStart="+$scope.accountAddressQueryInfo.dateStart+"&dateEnd="+$scope.accountAddressQueryInfo.dateEnd+"&pageNo="+$scope.accountAddressGridOptions.pageNo+"&pageSize="+$scope.accountAddressGridOptions.pageSize,
			{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(data){
			var copyColumn = angular.copy($scope.initAccountAddressGridColumn);
			$scope.accountAddressGrid.columnDefs = copyColumn;
			for(var key in data.data.tableHeader){
				var value = data.data.tableHeader[key];
				$scope.accountAddressGrid.columnDefs.push({"field":key,"displayName":value,"width":300,"sortable":false})
			}
			$scope.accountAddressGrid.columnDefs.push({"name":"action","displayName":"注册地","width":200,"sortable":false,cellTemplate:"<a class='lh30' ng-click='grid.appScope.showAccountAddressModal(row)' >注册地交易分布</a>"})
			$scope.accountAddressGrid.columnDefs.push({"field":"accountDate","displayName":"日期","width":200,"sortable":false,"cellFilter":'date:"yyyy-MM-dd"'})

			$scope.accountAddressGrid.totalItems = data.totalCount;//总记录数
			$scope.accountAddressGrid.data = data.data.gridData;
		}).error(function(){
		});
	};

	$scope.queryAccountAddress();


	$scope.accountAddressDetailGrid = {
		data: 'accountAddressDetailMappings',
		columnDefs: [
			{field: 'merProvince',displayName: '注册地',width: 200},
			{field: 'transAmount',displayName: '已跑量金额(万)',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.transAmount/10000).toFixed(2)}}</span>'},
			{field: 'accountDate',displayName: '日期',width: 150,cellFilter:'date:"yyyy-MM-dd"'}
		],
		onRegisterApi : function(gridApi) {
			$scope.accountAddressDetailGrid = gridApi;
		}
	};

	$scope.accountAddressDetail = {};
	$scope.showAccountAddressModal = function(row) {
		if(!row.entity.accountAddress){
			$scope.notice("账单地为空，无法查看详情");
			return ;
		}
		$scope.accountAddressDetail = {"address": row.entity.accountAddress,"date": row.entity.accountDate};
		$http.post("jumpRoute/accountAddressMonitorDetail.do",
			"address="+row.entity.accountAddress+"&date="+row.entity.accountDate,
			{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(data){
			if(data.status){
				$scope.accountAddressDetailMappings = data.data;
			}
		}).error(function(){
		});
		$('#accountAddressModal').modal('show');
	}



	//账单地预留额度监控
	$scope.accountAddressQuotaGrid = {
		paginationPageSize:10,                  //分页数量
		paginationPageSizes: [10,20,50,100],	//切换每页记录数
		useExternalPagination: true,		    //开启拓展名
		enableHorizontalScrollbar: true,        //横向滚动条
		enableVerticalScrollbar : true,  		//纵向滚动条
		columnDefs: [
			{field: 'address',displayName: '账单地',width: 150},
			{field: 'addressType',displayName: '是否单列市',width: 200,cellFilter:"formatDropping:"+angular.toJson($scope.addressType)},
			{field: 'releaseTime',displayName: '释放时间点',width: 150, cellFilter:'date:"HH:mm:ss"'},
			{field: 'reservedAmount',displayName: '预留额度',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.reservedAmount/10000).toFixed(2)}}</span>'},
			{field: 'localUsed',displayName: '账单地已用额度',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.localUsed/10000).toFixed(2)}}</span>'},
			{field: 'otherUsed',displayName: '其他地区已用额度',width: 150,cellTemplate:
					'<span class="lh30">{{(row.entity.otherUsed/10000).toFixed(2)}}</span>'},
			{field: 'accountDate',displayName: '日期',width: 150, cellFilter:'date:"yyyy-MM-dd"'}
		],
		onRegisterApi : function(gridApi) {
			gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
				$scope.accountAddressQuotaGridOptions.pageNo = newPage;
				$scope.accountAddressQuotaGridOptions.pageSize = pageSize;
				$scope.queryAccountAddressQuota();
			});
		}
	};



	// $scope.routeQueryInfo = {dateStart: "",dateEnd: ""};
	$scope.resetAccountAddressQuota = function() {
		$scope.accountAddressQuotaQueryInfo = {address:"",dateStart: moment(new Date()).subtract(15,'days').format("YYYY-MM-DD"),dateEnd: moment(new Date()).subtract(1,'days').format("YYYY-MM-DD"),addressType:-1};
	};
	$scope.resetAccountAddressQuota();

	$scope.queryAccountAddressQuota = function() {
		var url = "jumpRoute/accountAddressQuotaMonitor.do";
		$http.post(url,
			"addressType="+$scope.accountAddressQuotaQueryInfo.addressType+"&address="+$scope.accountAddressQuotaQueryInfo.address+"&dateStart="+$scope.accountAddressQuotaQueryInfo.dateStart+"&dateEnd="+$scope.accountAddressQuotaQueryInfo.dateEnd+"&pageNo="+$scope.accountAddressQuotaGridOptions.pageNo+"&pageSize="+$scope.accountAddressQuotaGridOptions.pageSize,
			{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
		).success(function(data){
			$scope.accountAddressQuotaGrid.data = data.result;
			$scope.accountAddressQuotaGrid.totalItems = data.totalCount;//总记录数
		}).error(function(){
		});
	};

	$scope.queryAccountAddressQuota();


	$scope.routeMonitorExport=function(){
		SweetAlert.swal({
				title: "确认导出？",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "提交",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			},
			function (isConfirm) {
				if (isConfirm) {
					var jumpId = $scope.routeQueryInfo.jumpId ? $scope.routeQueryInfo.jumpId : 0;
					var data = {
						"jumpId":jumpId,
						"dateStart":$scope.routeQueryInfo.dateStart,
						"dateEnd":$scope.routeQueryInfo.dateEnd
					}
					$scope.exportInfoClick("jumpRoute/routeMonitorExport.do",data);
				}
			});
	}


	$scope.regAddressMonitorExport=function(){
		SweetAlert.swal({
				title: "确认导出？",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "提交",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			},
			function (isConfirm) {
				if (isConfirm) {
					var data = {
						"address":$scope.addressQueryInfo.address,
						"dateStart":$scope.addressQueryInfo.dateStart,
						"dateEnd":$scope.addressQueryInfo.dateEnd
					}
					$scope.exportInfoClick("jumpRoute/regAddressMonitorExport.do",data);
				}
			});
	}


	$scope.accountAddressMonitorExport=function(){
		SweetAlert.swal({
				title: "确认导出？",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "提交",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			},
			function (isConfirm) {
				if (isConfirm) {
					var data = {
						"address":$scope.accountAddressQueryInfo.address,
						"dateStart":$scope.accountAddressQueryInfo.dateStart,
						"dateEnd":$scope.accountAddressQueryInfo.dateEnd
					}
					$scope.exportInfoClick("jumpRoute/accountAddressMonitorExport.do",data);
				}
			});
	}




	$scope.regAddressMonitorDetailExport=function(){
		SweetAlert.swal({
				title: "确认导出？",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "提交",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			},
			function (isConfirm) {
				if (isConfirm) {
					$scope.exportInfoClick("jumpRoute/addressMonitorDetailExport.do",$scope.addressDetailDate);
				}
			});
	}

	$scope.accountAddressMonitorDetailExport=function(){
		SweetAlert.swal({
				title: "确认导出？",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "提交",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			},
			function (isConfirm) {
				if (isConfirm) {
					$scope.exportInfoClick("jumpRoute/accountAddressMonitorDetailExport.do",$scope.accountAddressDetail);
				}
			});
	}

	$scope.accountAddressQuotaMonitorExport=function(){
		SweetAlert.swal({
				title: "确认导出？",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "提交",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			},
			function (isConfirm) {
				if (isConfirm) {
					var data = {
						"addressType":$scope.accountAddressQuotaQueryInfo.addressType,
						"address":$scope.accountAddressQuotaQueryInfo.address,
						"dateStart":$scope.accountAddressQuotaQueryInfo.dateStart,
						"dateEnd":$scope.accountAddressQuotaQueryInfo.dateEnd
					}
					$scope.exportInfoClick("jumpRoute/accountAddressQuotaMonitorExport.do",data);
				}
			});
	}

});